/* $RCSFile: ModuleListDataType.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/DataTypes/ModuleListDataType.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:29  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.IO;
using System.ComponentModel;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Configuration;
using AddonNice.Helpers;
using AddonNice.Diagnostics;
using System.Diagnostics;
using System.Globalization;

namespace AddonNice.UI.DataTypes
{
	/// <summary>
	/// ModuleListDataType
	/// </summary>
	public class ModuleListDataType : ListDataType
	{
        public bool     UseGuid     =   false;
        public int      ModuleID    =   0;
        public Guid[]   SharedGuiDs =   null;

		public ModuleListDataType(string ModuleType)
		{
			InnerDataSource         =   ModuleType;
		}

		public ModuleListDataType(int wModuleId,Guid[] wSharedGuiDs)
		{
            UseGuid                 =   true;
			ModuleID                =   wModuleId;
            SharedGuiDs             =   wSharedGuiDs;
            if (DataTypeTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ModuleListDataType Ctor ModuleID: {0}, SharedGuiDs.Length: {1}.", ModuleID,SharedGuiDs.Length),DataTypeTraceSwitch.Sw.Info);
	    }

		public override string DataValueField
		{
			get
			{
                if ( !UseGuid )
				    return "ModuleID";
                return "ID";
			}
			set
			{
				throw new ArgumentException("Value cannot be set", "ModuleID");
			}
		}

		public override string DataTextField
		{
			get
			{
                if ( !UseGuid )
    				return "ModuleTitle";
                return "Title";
			}
			set
			{
				throw new ArgumentException("Value cannot be set", "ModuleTitle");
			}
		}

        public override object DataSource
		{
			get
			{
                if (_DataSource == null)
                {
                    try
                    {
                        ModulesDB mdb               =   new ModulesDB();
                        if ( !UseGuid )
                        {
                            _DataSource             =   mdb.GetModulesByName(InnerDataSource.ToString(),portalSettings.PortalID);
                        }
                        else
                        {
                            ArrayList lst           =   new ArrayList();
                            foreach ( Guid SharedGuiD in SharedGuiDs )
                            {
                                ArrayList lst1      =   mdb.FindModulesByGuid(portalSettings.PortalID,SharedGuiD,ModuleID);
                                lst.AddRange(lst1);
                            }
                            ModuleItem mi           =   new ModuleItem();
                            mi.Title                =   LocalizeHelper.Localize( "MODULE_NOMODULE","No Module");
                            mi.ID                   =   0;
                            lst.Insert(0, mi);
                            _DataSource             =   lst;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (DataTypeTraceSwitch.Sw.TraceError)
                            Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ModuleListDataType DataSource GET ex: {0}", ex),DataTypeTraceSwitch.Sw.Info);
                        throw;
                    }
                }
                return _DataSource;
			}
		}
        
		public override string Description
		{
			get
			{
				return "Module List";
			}
		}
	}
}